<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="zzl"><meta name="copyright" content="zzl"><meta name="generator" content="Hexo 5.4.0"><meta name="theme" content="hexo-theme-yun"><title>jdbc | zzl</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.25/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_dxory92pb0h.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="icon" href="/web/yun.svg"><link rel="mask-icon" href="/web/yun.svg" color="#0078E7"><link rel="alternate icon" href="/web/yun.ico"><link rel="preload" href="/web/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/web/js/utils.js" as="script"><link rel="preload" href="/web/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/web/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"zilongzeng.gitee.io","root":"/web/","title":"奏者的小站","version":"1.6.2","mode":"auto","copycode":true,"page":{"isPost":true},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}.","hits":"${hits} results found","hits_time":"${hits} results found in ${time} ms"},"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><link rel="stylesheet" href="/web/css/hexo-theme-yun.css"><script src="/web/js/utils.js"></script><script src="/web/js/hexo-theme-yun.js"></script><meta name="description" content="jdbc课程记录与一些奇怪的sql">
<meta property="og:type" content="article">
<meta property="og:title" content="jdbc">
<meta property="og:url" content="http://zilongzeng.gitee.io/web/2022/04/28/jdbc/index.html">
<meta property="og:site_name" content="zzl">
<meta property="og:description" content="jdbc课程记录与一些奇怪的sql">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-04-28T06:42:30.000Z">
<meta property="article:modified_time" content="2022-04-28T06:45:43.436Z">
<meta property="article:author" content="zzl">
<meta property="article:tag" content="数据库">
<meta name="twitter:card" content="summary"><script src="/web/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest"></script><script defer src="/web/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/web/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="Table of Contents"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="Overview"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/web/about/" title="zzl"><img width="96" loading="lazy" src="/web/zzl.jpg" alt="zzl"><span class="site-author-status" title="永远相信美好的事情即将发生">😊</span></a><div class="site-author-name"><a href="/web/about/">zzl</a></div><span class="site-name">zzl</span><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/web/" title="Home"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/web/archives/" title="Archives"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">16</span></a></div><div class="site-state-item"><a href="/web/categories/" title="Categories"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">0</span></a></div><div class="site-state-item"><a href="/web/tags/" title="Tags"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="/atom.xml" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ 2915667803" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微信公众号" target="_blank" style="color:#1AAD19"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-2-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-train-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/web/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JDBC"><span class="toc-number">1.</span> <span class="toc-text">JDBC</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JDBC%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C%E5%9F%BA%E6%9C%AC%E6%AD%A5%E9%AA%A4"><span class="toc-number">1.1.</span> <span class="toc-text">JDBC数据库操作基本步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%BC%96%E5%86%99"><span class="toc-number">1.2.</span> <span class="toc-text">代码编写</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SQL%E6%B3%A8%E5%85%A5"><span class="toc-number">2.</span> <span class="toc-text">SQL注入</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#day-3-28"><span class="toc-number">3.</span> <span class="toc-text">day 3.28</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BD%AF%E4%BB%B6%E7%9A%84%E5%88%86%E5%B1%82%E8%AE%BE%E8%AE%A1MVC%E6%A8%A1%E5%BC%8F"><span class="toc-number">3.1.</span> <span class="toc-text">软件的分层设计MVC模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E6%A1%88%E4%BE%8B%E2%80%93%E5%AD%A6%E7%94%9F%E4%BF%A1%E6%81%AF%E7%AE%A1%E7%90%86"><span class="toc-number">3.2.</span> <span class="toc-text">实现案例–学生信息管理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%8D%E5%90%88%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%93%8D%E4%BD%9C"><span class="toc-number">4.</span> <span class="toc-text">复合对象的操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MyBatis"><span class="toc-number">5.</span> <span class="toc-text">MyBatis</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#mybatis%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">6.</span> <span class="toc-text">mybatis的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%93%8D%E4%BD%9C%EF%BC%88mybatis%E4%BD%BF%E7%94%A8%EF%BC%89"><span class="toc-number">7.</span> <span class="toc-text">对数据库的操作（mybatis使用）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%B9%E9%87%8F%E5%88%A0%E9%99%A4"><span class="toc-number">7.1.</span> <span class="toc-text">批量删除</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E6%9D%A1%E4%BB%B6%E7%BB%84%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="toc-number">7.2.</span> <span class="toc-text">多条件组合查询</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%8D%E5%90%88%E5%AF%B9%E8%B1%A1"><span class="toc-number">8.</span> <span class="toc-text">复合对象</span></a></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="hty-card post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://zilongzeng.gitee.io/web/web/2022/04/28/jdbc/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="zzl"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="zzl"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">jdbc</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="Created: 2022-04-28 14:42:30" itemprop="dateCreated datePublished" datetime="2022-04-28T14:42:30+08:00">2022-04-28</time></div><div class="post-classify"><span class="post-tag"><a class="tag-item" href="/web/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">数据库</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h2 id="JDBC"><a href="#JDBC" class="headerlink" title="JDBC"></a>JDBC</h2><hr>
<h3 id="JDBC数据库操作基本步骤"><a href="#JDBC数据库操作基本步骤" class="headerlink" title="JDBC数据库操作基本步骤"></a>JDBC数据库操作基本步骤</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1、加载符合要访问的数据库的驱动（将jar包拷贝到项目）</span></span><br><span class="line">Class.forName(<span class="string">&quot;&quot;</span>)</span><br><span class="line"><span class="comment">// 2、创建连接Connection</span></span><br><span class="line">Connection conn = DirverManager.getConnection(url,user,pwd)</span><br><span class="line"><span class="comment">// 3、创建Statement</span></span><br><span class="line">Statement stmt = conn.createStatement();</span><br><span class="line"><span class="comment">// 4、执行sql语句</span></span><br><span class="line"><span class="comment">// 查询</span></span><br><span class="line">ResultSet rs = stmt.executeQuery(sql)</span><br><span class="line"><span class="comment">//更新</span></span><br><span class="line"><span class="keyword">int</span> n = stmt.executeUpdate(sql)</span><br><span class="line"><span class="comment">// 5、处理结果</span></span><br><span class="line"><span class="comment">//遍历或取第一条记录</span></span><br><span class="line"><span class="keyword">while</span>(rs.next())&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span>(rs.next())&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 6、关闭连接，释放资源</span></span><br><span class="line">conn.close()</span><br></pre></td></tr></table></figure>

<h3 id="代码编写"><a href="#代码编写" class="headerlink" title="代码编写"></a>代码编写</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 实现对学生成绩的查询</span></span><br><span class="line"><span class="comment">//创建一个项目（java project）：eduSys</span></span><br><span class="line"><span class="comment">//功能1：用户输入学号，输出该学生的平均成绩</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="SQL注入"><a href="#SQL注入" class="headerlink" title="SQL注入"></a>SQL注入</h2><hr>
<p>对于statement,当进行查询的时候，容易被攻击者在变量里输入一些特殊的文字，导致SQL执行出现偏差</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span>   <span class="operator">*</span> <span class="keyword">from</span>  表名  <span class="keyword">where</span>  字段<span class="number">1</span><span class="operator">=</span> xxx   <span class="keyword">and</span>   字段<span class="number">2</span> <span class="operator">=</span> xxxx </span><br><span class="line">以登陆为例</span><br><span class="line">  (<span class="number">1</span>)新建账号表 account  ( id,username,password)</span><br><span class="line">     录入几条测试账号</span><br><span class="line">  (<span class="number">2</span>)编写登陆 方法login()</span><br><span class="line"><span class="keyword">select</span>  <span class="operator">*</span> <span class="keyword">from</span> account   </span><br><span class="line"><span class="keyword">where</span>   username <span class="operator">=</span><span class="string">&#x27;admin&#x27;</span>      <span class="keyword">and</span>      password  <span class="operator">=</span> <span class="string">&#x27;123&#x27;</span>   </span><br><span class="line">知识点：<span class="keyword">select</span> 查询  的本质： </span><br><span class="line">对要查询的表里面的每一条记录，将给定字段值代入到<span class="keyword">where</span></span><br><span class="line">条件里面进行逻辑运算，如果结果为<span class="literal">true</span>则该条记录被选中，</span><br><span class="line">如果为<span class="literal">false</span>，则该条记录舍弃</span><br><span class="line"></span><br><span class="line">输入特殊的用户名  <span class="number">1</span><span class="string">&#x27;=1 or 1=1 or &#x27;</span><span class="number">1</span><span class="operator">=</span><span class="number">1</span></span><br><span class="line">密码任意输入</span><br><span class="line">执行的<span class="keyword">sql</span>语句</span><br><span class="line"><span class="keyword">select</span>  <span class="operator">*</span> <span class="keyword">from</span> account    </span><br><span class="line"><span class="keyword">where</span>   username <span class="operator">=</span><span class="string">&#x27;1&#x27;</span>  <span class="keyword">or</span> <span class="number">1</span><span class="operator">=</span><span class="number">1</span> <span class="keyword">or</span> <span class="string">&#x27;1&#x27;</span> <span class="operator">=</span>  <span class="string">&#x27;1&#x27;</span>   </span><br><span class="line">           <span class="keyword">and</span>      password  <span class="operator">=</span><span class="string">&#x27;llll&#x27;</span>      </span><br><span class="line"><span class="comment">-------- 永真表达式</span></span><br></pre></td></tr></table></figure>
<p><code>Statement</code> 缺点：带条件查询，容易被用户输入特定的文字，导致sql的执行逻辑出现偏差，被恶意sql注入</p>
<p><code>PreparedStatement</code> :将要执行的sql语句事先编译好（固定好执行逻辑，不会受传递的变量的值的影响）</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">1</span>)先确定好要执行的<span class="keyword">sql</span>，需要传递变量的地方，用?来占位</span><br><span class="line"> <span class="keyword">select</span>   <span class="operator">*</span> <span class="keyword">from</span> account  </span><br><span class="line"><span class="keyword">where</span>  username <span class="operator">=</span> ?   <span class="keyword">and</span> password <span class="operator">=</span> ?</span><br><span class="line">(<span class="number">2</span>)根据<span class="keyword">sql</span>语句,创建PreparedStatement对象</span><br><span class="line">(<span class="number">3</span>)给?所占位的地方，设置给定的值</span><br><span class="line">(<span class="number">4</span>)开始执行</span><br></pre></td></tr></table></figure>

<h2 id="day-3-28"><a href="#day-3-28" class="headerlink" title="day 3.28"></a>day 3.28</h2><h3 id="软件的分层设计MVC模式"><a href="#软件的分层设计MVC模式" class="headerlink" title="软件的分层设计MVC模式"></a>软件的分层设计MVC模式</h3><p>将整个软件从逻辑上，分成若干个业务单元<br>（1）View 视图层：只负责数据的采集和数据的展示<br>（2）Controller 控制层：根据View传递过来的请求进行具体业务的处理<br>（3）Dao 数据访问层：负责数据库中数据的存取，每个表对应一个Dao类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//一般提供5个方法</span></span><br><span class="line"><span class="number">1</span>、getAll():获取所有数据</span><br><span class="line"><span class="number">2</span>、getById():根据id获取某个特定的数据</span><br><span class="line"><span class="number">3</span>、add()</span><br><span class="line"><span class="number">4</span>、update()</span><br><span class="line"><span class="number">5</span>、delete()</span><br></pre></td></tr></table></figure>
<p>（4）Model 模型：具体的业务对象</p>
<h3 id="实现案例–学生信息管理"><a href="#实现案例–学生信息管理" class="headerlink" title="实现案例–学生信息管理"></a>实现案例–学生信息管理</h3><p>（1）创建四个包（package），存放每层的类<br>  model，dao，controller，view<br>（2）在model包里，定义学生业务对象类Student与学生表对应<br>（3）在dao包里面，定义学生信息的数据库访问类StudentDao来负责完成学生信息的数据库表的存取<br>（4）在controller包里面，定义学生业务控制器StudentController<br>（5）在view包里面，定义学生信息管理界面StudentUI</p>
<hr>
<h2 id="复合对象的操作"><a href="#复合对象的操作" class="headerlink" title="复合对象的操作"></a>复合对象的操作</h2><p>当某个表A含有外键，对应表B，需要在显示A中内容的时候，把B里面 某些内容也显示出来，在设计业务实体类的时候，在A中定义一个指向B对象的一个属性 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span></span>&#123;                      </span><br><span class="line">  &#125;  </span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span></span>&#123;</span><br><span class="line">   <span class="keyword">private</span>  B  b;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>在获得A对象信息的时候，调用B所对应的Dao 通过外键获得B对象</p>
<p>实现案例： 在显示学生列表的时候，显示对应班级的名称等信息</p>
<p>1、定义班级实体类</p>
<p>2、定义班级DAO类 ClassesDao 并定义 getById()方法</p>
<p>3、在学生Dao类中获得学生信息的地方，根据外键classer_id调用ClassesDao对象的getById()的方法，获得对象，赋值给学生对象</p>
<h2 id="MyBatis"><a href="#MyBatis" class="headerlink" title="MyBatis"></a>MyBatis</h2><p>在MVC三层架构中，最大的编码工作在Dao层（完成实体对象与数据库表记录之间的交互），利用MyBatis框架，不需要编写繁琐的数据库访问代码，只需要配置sql映射文件。</p>
<p>MyBatis核心：</p>
<ul>
<li><p>（1）数据库访问类Mapper的定义：只需要定义数据库访问方法的原型，不需要实现</p>
</li>
<li><p>（2）数据库访问类Mapper的sql映射xml文件：定义方法需要访问的sql语句</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">以 学院信息管理 为例</span><br><span class="line"></span><br><span class="line"> (<span class="number">1</span>)定义学院实体类 College</span><br><span class="line"></span><br><span class="line"> (<span class="number">2</span>)定义学院数据访问接口 CollegeMapper</span><br><span class="line"></span><br><span class="line"> (<span class="number">3</span>)编写CollegeMapper对应的xml映射文件：对CollegeMapper接口里面所声明的各种方法进行说明要执行sql的语句</span><br><span class="line"></span><br><span class="line"> (<span class="number">4</span>)将mybatis 所需要的jar包 拷贝到项目</span><br><span class="line"></span><br><span class="line"> (<span class="number">5</span>)配置mybatis运行所需要的主配置文件 mybatis-config.xml</span><br><span class="line"></span><br><span class="line"> (<span class="number">6</span>)编写CollegeController，定义getAll方法：使用Mybatis的 session机制</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="mybatis的使用"><a href="#mybatis的使用" class="headerlink" title="mybatis的使用"></a>mybatis的使用</h2><p> mybatis提供一套session(会话）机制，来实现与程序的交互<br>（1） 利用给定的mybatis-config.xml配置文件，来初始化mybatis<br>（2）  建立与mybatis的session连接<br>（3） 将Mapper接口 实例化一个对象<br>（4） 调用接口里面的方法<br>（5） 提交事务commit  —-增加、删除、修改操作涉及<br>（6） 释放与mybatis的session连接</p>
<p>注意：涉及的路径问题<br>（1） MybatisUtil里面对mybatis-config.xml的访问<br>（2） mybatis-config.xml里面<br>  1)typeAliases 里面package 指明model所在的包<br>  2)mapper resource 指明的 mapper xml文件路径<br>（3）mapper xml文件里面 namespace=“”<br>    指明 mapper所在的类名（全路径带 包名）</p>
<h2 id="对数据库的操作（mybatis使用）"><a href="#对数据库的操作（mybatis使用）" class="headerlink" title="对数据库的操作（mybatis使用）"></a>对数据库的操作（mybatis使用）</h2><h3 id="批量删除"><a href="#批量删除" class="headerlink" title="批量删除"></a>批量删除</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">--批量处理： 传递过来的是集合(数组，动态数组，Map等），通过&lt;foreach&gt;对集合进行遍历，生成 (  xx, xx,xx ) 文字 追加到sql语句</span></span><br><span class="line">基本用法</span><br><span class="line">   <span class="operator">&lt;</span>foreach  collection<span class="operator">=</span>&quot;集合的类型&quot;   item<span class="operator">=</span>&quot;变量名&quot;   </span><br><span class="line"><span class="keyword">open</span><span class="operator">=</span>&quot;&quot;   <span class="keyword">close</span><span class="operator">=</span>&quot;&quot;   seperator<span class="operator">=</span>&quot;&quot; <span class="operator">&gt;</span></span><br><span class="line"></span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">/</span>foreach<span class="operator">&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="多条件组合查询"><a href="#多条件组合查询" class="headerlink" title="多条件组合查询"></a>多条件组合查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">--定义一个方法，该方法要执行的sql中的条件，根据用户的输入来决定（用户选择了某个条件，在where才带上该条件）</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表</span><br><span class="line"><span class="keyword">where</span> [条件<span class="number">1</span>],[条件<span class="number">2</span>]...</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">---在where sql 语句中，根据用户输入的参数，如果有，则条件出现，使用：</span></span><br><span class="line"><span class="operator">&lt;</span>if test<span class="operator">=</span>&quot;某个参数!=null&quot;<span class="operator">&gt;</span></span><br><span class="line">    <span class="keyword">and</span> 与参数相关联的条件</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">/</span>if<span class="operator">&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<hr>
<h2 id="复合对象"><a href="#复合对象" class="headerlink" title="复合对象"></a>复合对象</h2><p>读取对象A信息的同时，将B的信息也加载进来</p>
<p>A:B   （1）1：1 （2）n:1  （3） 1：n</p>
<p>通过mybatis配置复合对象的读取实现</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--1、在显示教师信息的时候，同时加载所在学院信息</span></span><br><span class="line"><span class="comment">--  教师：学院  ==》 n:1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">--实现步骤：</span></span><br><span class="line"><span class="comment">--(1)创建教师实体Teacher</span></span><br><span class="line"><span class="comment">--(2)创建教师数据访问接口 TeacherMapper 以及映射文件TeacherMapper.xml,并在mybatis-config.xml手动注册</span></span><br><span class="line"><span class="comment">--(3)编写教师控制器类TeacherController</span></span><br><span class="line"></span><br><span class="line"><span class="comment">--补充：mybatis内部采用缓存机制，对于从数据库中读取后生成的对象，会储存到缓存中，当下次再读取同一个id对象的时候，mybatis直接从缓存中提取，不会访问数据库。</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">--2、在显示某个班级详细信息的时候，同时加载该班级所有学生</span></span><br><span class="line"><span class="comment">--班级：学生  ==》  1：n</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="Donate" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">I want you.</div><div id="qr" style="display:none;"><div style="display:inline-block"></div><div style="display:inline-block"></div><div style="display:inline-block"></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>Post author: </strong>zzl</li><li class="post-copyright-link"><strong>Post link: </strong><a href="http://zilongzeng.gitee.io/web/2022/04/28/jdbc/" title="jdbc">http://zilongzeng.gitee.io/web/2022/04/28/jdbc/</a></li><li class="post-copyright-license"><strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> unless otherwise stated.</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/web/2022/04/28/sql%E8%AF%AD%E8%A8%80/" rel="prev" title="sql语言"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">sql语言</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/web/2022/01/30/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" rel="next" title="数据类型"><span class="post-nav-text">数据类型</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div class="hty-card" id="comment"><div class="comment-tooltip text-center"><span>要不要和我说些什么？</span><br></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> zzl</span></div><div class="powered"><span>Powered by <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> v5.4.0</span><span class="footer-separator">|</span><span>Theme - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.6.2</span></div></footer><a class="hty-icon-button" id="back-to-top" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a></div></body></html>